package mn.slide.laborberlin.main;

import java.util.ArrayList;

import mn.slide.laborberlin.adaptor.AnspachAdapter;
import mn.slide.laborberlin.db.DataLayer;
import mn.slide.laborberlin.model.PListMainModel;
import mn.slide.laborberlin.tabgroup.GroupStarter;
import mn.slide.laborberlin.tabgroup.HomeGroupActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class AnsprechActivity extends Activity {
	private ListView listV;
	ArrayList<PListMainModel> mainList;
	// private Button back_btn;
	EditText search_edt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ansprech);
		listV = (ListView) findViewById(R.id.main_list);
		View footerView = ((LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.merklist_header, null, false);
		listV.addHeaderView(footerView);

		TextView header = (TextView) findViewById(R.id.header_title);
		header.setText(R.string.ansprechpartner_fachbereiche);
		mainList = DataLayer.layer.getContactList();

		search_edt = (EditText) findViewById(R.id.search_edt);
		search_edt.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// If the event is a key-down event on the "enter" button
				if ((event.getAction() == KeyEvent.ACTION_DOWN)
						&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
					String k = search_edt.getText().toString();
					if (k.length() > 0) {

						Intent intent = new Intent(AnsprechActivity.this,
								SearchActivity.class);
						intent.putExtra("searchTxt", k);
						HomeGroupActivity.group.goTo(intent);

					}
					return true;
				}
				return false;
			}
		});
		// back_btn = (Button) findViewById(R.id.back_btn);
		// back_btn.setOnClickListener(new View.OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// onBackPressed();
		//
		// }
		// });
		listV.setAdapter(new AnspachAdapter(this, mainList));

	}

	@Override
	public void onBackPressed() {

		GroupStarter.getInstance().onBackpressed(this);
	}
	// private String readPlistFromAssets() {
	// StringBuffer sb = new StringBuffer();
	// BufferedReader br = null;
	// try {
	// br = new BufferedReader(new InputStreamReader(getAssets().open(
	// "kontakts.plist")));
	// String temp;
	// while ((temp = br.readLine()) != null)
	// sb.append(temp);
	// } catch (IOException e) {
	// e.printStackTrace();
	// } finally {
	// try {
	// br.close(); // stop reading
	// } catch (IOException ex) {
	// ex.printStackTrace();
	// }
	// }
	// return sb.toString();
	// }

}
